import cn.hutool.core.util.*;

public class FilterEmpty {

    public static void main(String[] args) {
        {
            String input = " 你好121 1世界 ";
            String result = filter(input);
            System.out.println(result);
        }
        {
            String input = " 你好12工 1界 ";
            String result = filter(input);
            System.out.println(result);
        }
        {
            String input = " 你好1211 世界 ";
            String result = filter(input);
            System.out.println(result);
        }

        {
            String input = " 你好123啊 世界 ";
            String result = filter(input);
            System.out.println(result);
        }

        {
            String input = " 你好12b 1界 ";
            String result = filter(input);
            System.out.println(result);
        }
        {
            String input = " 你好1211 b世界 ";
            String result = filter(input);
            System.out.println(result);
        }
    }

    public static String filter(String question) {

        if(ObjectUtil.isNotEmpty(question)){
            try {
                //中间
                StringBuffer sb = new StringBuffer();
                char []charArr = {' '};

                for (int i = 0; i < question.length(); i++) {
                    char ch = question.charAt(i);
                    boolean filter = false;
                    if(ArrayUtil.contains(charArr,ch)){
                        if((i-1)>0&&(i+1)<question.length()) {
                            char start = question.charAt(i - 1);
                            char end = question.charAt(i + 1);
                            //非（开头和结尾都是数字）
                            if (!(NumberUtil.isNumber(Character.toString(start)) && NumberUtil.isNumber(Character.toString(end)))) {
                                filter = true;
                            }
                            //开头字母不过滤
                            if ((start>=65&&start<=90) || (start>=97&&start<=122) ) {
                                filter = false;
                            }
                            //结尾是字母不过滤
                            if ((end>=65&&end<=90) || (end>=97&&end<=122) ) {
                                filter = false;
                            }
                            ///开头不是空格，或结尾不是空格
                            if (ArrayUtil.contains(charArr, start) || ArrayUtil.contains(charArr, end)) {
                                filter = true;
                            }
                        }
                    }

                    if(!filter) {
                        sb.append(ch);
                    }
                }

                String newQuestion = sb.toString();
                //边界空格处理
                newQuestion = newQuestion.trim();
                String startChar = newQuestion.substring(0,1);
                String endChar = newQuestion.substring(newQuestion.length()-1,newQuestion.length());
                if(NumberUtil.isNumber(startChar)&&NumberUtil.isNumber(endChar)){
                    newQuestion = " "+newQuestion+" ";
                }
                return newQuestion;
            }catch (Exception e){
                return question;
            }
        }
        return question;
    }




}
